package com.uduemc.biso.master.backend.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uduemc.biso.core.extities.center.AuthorizeModel;
import com.uduemc.biso.core.extities.center.AuthorizeModelAction;
import com.uduemc.biso.core.utils.JsonResult;
import com.uduemc.biso.master.backend.service.AuthorizeService;

/**
 * 用户授权模块
 * 
 * @author guanyi
 *
 */
@Controller
@RequestMapping("/backend/authorize")
public class BAuthorizeController {

	@Autowired
	private AuthorizeService authorizeServiceImpl;

	@Autowired
	private ObjectMapper objectMapper;

	/**
	 * 权限列表，包含模块以及动作列表
	 * 
	 * @param modelAndView
	 * @return
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@GetMapping(value = { "/list" })
	public ModelAndView list(ModelAndView modelAndView)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<AuthorizeModel> modelInfos = authorizeServiceImpl.getModelInfos();
		modelAndView.addObject("modelDatas", modelInfos);
		modelAndView.addObject("modelInfos", objectMapper.writeValueAsString(modelInfos));
		modelAndView.setViewName("backend/authorize/list");
		return modelAndView;
	}

	@GetMapping(value = { "/list/model/infos" })
	@ResponseBody
	public List<AuthorizeModel> modelData()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<AuthorizeModel> modelInfos = authorizeServiceImpl.getModelInfos();
		return modelInfos;
	}

	@GetMapping(value = { "/list/model/action/infos" })
	@ResponseBody
	public List<AuthorizeModelAction> modelActionData()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<AuthorizeModelAction> modelInfos = authorizeServiceImpl.getModelActionInfos();
		return modelInfos;
	}

	@GetMapping(value = { "/list/model/action/infosbymodelid/{modelId:\\d+}" })
	@ResponseBody
	public List<AuthorizeModelAction> modelActionDataByModelId(@PathVariable("modelId") long modelId)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<AuthorizeModelAction> modelInfos = authorizeServiceImpl.getModelActionInfosByModelId(modelId);
		return modelInfos;
	}

	@PostMapping(value = { "/list/model/action/insert" })
	@ResponseBody
	public Object modelActionInsert(@RequestParam("modelId") long modelId, @RequestParam("type") short type,
			@RequestParam("name") String name, @RequestParam("keyword") String keyword,
			@RequestParam("orderNum") int orderNum, @RequestParam("remark") String remark)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		AuthorizeModelAction insertData = new AuthorizeModelAction();
		insertData.setModelId(modelId).setType(type).setName(name).setKeyword(keyword).setRemark(remark)
				.setOrderNum(orderNum);
		AuthorizeModelAction data = authorizeServiceImpl.insertAuthorizeModelAction(insertData);
		return JsonResult.messageSuccess(data);
	}

	@PostMapping(value = { "/list/model/action/update" })
	@ResponseBody
	public Object modelActionUpdate(@RequestParam("id") long id, @RequestParam("modelId") long modelId,
			@RequestParam("type") short type, @RequestParam("name") String name,
			@RequestParam("keyword") String keyword, @RequestParam("orderNum") int orderNum,
			@RequestParam("remark") String remark)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		AuthorizeModelAction authorizeModelAction = authorizeServiceImpl.getAuthorizeModelActionInfoById(id);
		if (authorizeModelAction == null) {
			return JsonResult.illegal();
		}
		authorizeModelAction.setModelId(modelId).setType(type).setName(name).setKeyword(keyword).setRemark(remark)
				.setOrderNum(orderNum);
		AuthorizeModelAction data = authorizeServiceImpl.updateAuthorizeModelAction(authorizeModelAction);
		return JsonResult.messageSuccess("更新成功！", data);
	}

	@PostMapping(value = { "/list/model/action/delete" })
	@ResponseBody
	public Object modelActionUpdate(@RequestParam("id") long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		AuthorizeModelAction authorizeModelAction = authorizeServiceImpl.getAuthorizeModelActionInfoById(id);
		if (authorizeModelAction == null) {
			return JsonResult.illegal();
		}
		boolean deleteAuthorizeModelAction = authorizeServiceImpl.deleteAuthorizeModelAction(id);
		if (!deleteAuthorizeModelAction) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("删除成功！", authorizeModelAction);
	}

	/**
	 * 权限组，将列表中的内容通过多对一的方式对应到权限组当中
	 * 
	 * @param modelAndView
	 * @return
	 */
	@GetMapping(value = { "/group" })
	public ModelAndView group(ModelAndView modelAndView) {

		modelAndView.setViewName("backend/authorize/group");
		return modelAndView;
	}
	
	/**
	 * 用户权限，给用户分配权限组
	 * 
	 * @param modelAndView
	 * @return
	 */
	@GetMapping(value = { "/usersetgp" })
	public ModelAndView user(ModelAndView modelAndView) {

		modelAndView.setViewName("backend/authorize/usersetgp");
		return modelAndView;
	}
}
